package com.tjn.generic;

import java.util.*;

/**
 * @author 陶镓妮
 * @version 1.0
 */
@SuppressWarnings("all")
public class GenericExercise {

    public static void main(String[] args) {

        HashSet<Student> hashSet = new HashSet<Student>();
        hashSet.add(new Student("tjn", 18));
        hashSet.add(new Student("dy", 20));
        hashSet.add(new Student("syz", 40));

        System.out.println("===hashSet增强for循环===");
        for (Student student : hashSet) {
            System.out.println(student.getName() + " " + student.getAge());
        }
        System.out.println("===hashSet迭代器循环===");
        Iterator<Student> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            System.out.println(student.getName() + " " + student.getAge());
        }

        HashMap<String, Student> hashMap = new HashMap<String, Student>();
        hashMap.put("tjn", new Student("tjn", 18));
        hashMap.put("dy", new Student("dy", 20));
        hashMap.put("syz", new Student("syz", 40));

        System.out.println("===hashMap迭代器===");
        Set<Map.Entry<String, Student>> entries = hashMap.entrySet();
        //这里因为创建对象时已经写了<String,Student>，所以这里迭代器里面自动填充的返回的也是<String,Student>
        Iterator<Map.Entry<String, Student>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<String, Student> next = iterator1.next();
            System.out.println(next.getKey() + "-" + next.getValue());
        }


    }
}

class Student{
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}